import requests

# 登录页面的 url
login_page_url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'

headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/123.0.0.0 Safari/537.36'
    }

login_page_response = requests.get(url=login_page_url, headers=headers)

login_page_text = login_page_response.text

# 解析页面源码  获取__VIEWSTATE  __VIEWSTATEGENERATOR
from bs4 import BeautifulSoup

login_page_soup = BeautifulSoup(login_page_text, 'lxml')

# 获取 __VIEWSTATE
viewstate = login_page_soup.select('#__VIEWSTATE')[0].attrs.get('value')

# 获取 __VIEWSTATEGENERATOR
viewstategenerator = login_page_soup.select('#__VIEWSTATEGENERATOR')[0].attrs.get('value')

# 获取验证码图片 有坑
src = login_page_soup.select('#imgCode')[0].attrs.get('src')
image_path = 'https://so.gushiwen.cn'+src
# print(image_path)

# requests 里面有一个 方法 session() 通过 session 的返回值 就能使请求变成一个对象
session = requests.session()
response_code = session.get(image_path)
# 下载文件要使用 流 也就是 二进制
code_content = response_code.content
with open('code.jpg', 'wb') as f:
    f.write(code_content)





